package com.yy.bolg.service.impl;

import com.yy.bolg.dao.CustomerDao;
import com.yy.bolg.entity.CustomerBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 客户Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-11-02
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public void add(CustomerBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsCustomerName())){
            throw new LackParameterException("客户名称不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerProvince()) || StringUtils.isEmpty(bean.getPsCustomerCity()) || StringUtils.isEmpty(bean.getPsCustomerRegion())){
            throw new LackParameterException("客户常驻地区不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerAddress())){
            throw new LackParameterException("客户详细地址不能为空");
        }

        if(bean.getPsCustomerLongitude() <= 0){
            throw new LackParameterException("经度不合法");
        }

        if(bean.getPsCustomerLatitude() <= 0){
            throw new LackParameterException("纬度不合法");
        }

        if(bean.getPsCustomerCategory() < 1){
            throw new LackParameterException("客户类别不合法");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserName())){
            throw new LackParameterException("管理人姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserPhone())){
            throw new LackParameterException("管理人手机号不能为空");
        }

        if (null != customerDao.findOneByPhone(bean.getPsCustomerUserPhone())) {
            throw new OtherThingsException("该手机号已被使用");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserPwd())){
            throw new LackParameterException("管理人密码不能为空");
        }

         if(StringUtils.isEmpty(bean.getPsCustomerUserHead())){
            throw new LackParameterException("管理人头像不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserPosition())){
            throw new LackParameterException("管理人职务不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerLicense())){
            throw new LackParameterException("客户营业执照不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerEmergencyUserName())){
            throw new LackParameterException("紧急联系人姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerEmergencyUserPhone())){
            throw new LackParameterException("紧急联系人手机号不能为空");
        }

        try {
            customerDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public CustomerBean findOneByCode(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        try {
            return customerDao.findOneByCode(bean.getPsCustomerCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public CustomerBean findOneByHikUnitId(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerUnitid())) {
            throw new LackParameterException("客户海康单位ID不能为空");
        }

        try {
            return customerDao.findOneByHikUnitId(bean.getPsCustomerUnitid());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public CustomerBean findOneByPhone(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        try {
            return customerDao.findOneByPhone(bean.getPsCustomerUserPhone());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public CustomerBean findOneByEmergencyPhone(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerEmergencyUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        try {
            return customerDao.findOneByEmergencyPhone(bean.getPsCustomerEmergencyUserPhone());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public List<CustomerBean> findAllByCondition(CustomerBean bean) throws Exception {
        try {
            return customerDao.findAllByCondition(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<CustomerBean> findAll() throws Exception {
        try {
            return customerDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public void del(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            customerDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }

    @Override
    public void up(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            customerDao.update(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public String signIn(CustomerBean bean) throws Exception {
        String code = "";

        if (StringUtils.isEmpty(bean.getPsCustomerUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsCustomerUserPwd())) {
            throw new LackParameterException("密码不能为空");
        }

        CustomerBean customerBean = customerDao.findOneByPhone(bean.getPsCustomerUserPhone());

        if (null == customerBean) {
            throw new OtherThingsException("手机号或密码错误");
        }else{
            if(!bean.getPsCustomerUserPhone().equals(customerBean.getPsCustomerUserPhone()) || !bean.getPsCustomerUserPwd().equals(customerBean.getPsCustomerUserPwd())){
                throw new OtherThingsException("手机号或密码错误");
            }else if(customerBean.getPsCustomerState() == 1){
                throw new OtherThingsException("账号审核中");
            }else if(customerBean.getPsCustomerState() == 3){
                throw new OtherThingsException("账号未通过审核");
            }else{
                try {
                    code = customerBean.getPsCustomerCode();
                    customerDao.signIn(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new OtherThingsException("登录失败");
                }
            }
        }
        return code;
    }

    @Override
    public void signOut(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            customerDao.signOut(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("退出失败");
        }
    }

    @Override
    public void upState(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if (bean.getPsCustomerState() == 2) {
            if(StringUtils.isEmpty(bean.getPsCustomerUnitid())){
                throw new LackParameterException("海康单位ID不能为空");
            }
        }

        try {
            customerDao.upState(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public void bindHik(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if (StringUtils.isEmpty(bean.getPsCustomerUnitid())) {
            throw new LackParameterException("海康单位ID不能为空");
        }

        try {
            customerDao.bindHik(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public void upCustomerUserInfo(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == customerDao.findOneByPhone(bean.getPsCustomerUserPhone())) {
            throw new OtherThingsException("请输入正确的原手机号");
        }

        /*if (StringUtils.isEmpty(bean.getGeneralPhone())) {
            throw new LackParameterException("新手机号不能为空");
        }

        if (bean.getGeneralPhone().length() != 11) {
            throw new LackParameterException("新手机号不合法");
        }*/

        try {
            customerDao.upCustomerUserInfo(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public void upCustomerEmergencyUserInfo(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerEmergencyUserPhone())) {
            throw new LackParameterException("原手机号不能为空");
        }

        if (null == customerDao.findOneByEmergencyPhone(bean.getPsCustomerEmergencyUserPhone())) {
            throw new OtherThingsException("请输入正确的原手机号");
        }

        /*if (StringUtils.isEmpty(bean.getGeneralPhone())) {
            throw new LackParameterException("新手机号不能为空");
        }

        if (bean.getGeneralPhone().length() != 11) {
            throw new LackParameterException("新手机号不合法");
        }*/

        try {
            customerDao.upCustomerEmergencyUserInfo(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public void upCustomerInfo(CustomerBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        if (null == customerDao.findOneByCode(bean.getPsCustomerCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            customerDao.upCustomerInfo(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }

    }
}
